import { createRouter, createWebHashHistory } from "vue-router";
import list from "@/views/list";
import detail from "@/views/detail.vue";
import add from "@/views/add.vue";
import login from "@/views/login.vue";
import management from "@/views/management.vue";
import managementList from "@/components/mangement/list.vue";

const routes = [
    {
        name: "list",
        path: "/list/:type",
        component: list,
        props: true,
        meta: {
            title: "学习笔记",
        },
        beforeEnter(to, from, next) {
            if (to.params.type === "project") to.meta.title = "项目笔记";
            next();
        },
    },
    {
        name: "index",
        path: "/",
        component: list,
        meta: {
            type: "study",
            title: "首页",
        },
    },
    {
        name: "detail",
        path: "/detail/:articleId",
        component: detail,
        props: true,
        meta: {
            title: "笔记详情",
        },
    },
    {
        name: "add",
        path: "/add",
        component: add,
        meta: {
            title: "添加笔记",
        },
    },
    {
        name: "login",
        path: "/login",
        component: login,
        meta: {
            title: "登录",
        },
    },
    {
        name: "management",
        path: "/management",
        component: management,
        meta: {
            title: "笔记管理",
        },
        redirect: { name: "managementList", params: { type: "study" } },
        children: [
            {
                name: "managementList",
                path: "list/:type",
                component: managementList,
                props: true,
            },
        ],
    },
];

const router = createRouter({
    history: createWebHashHistory(),
    routes,
});

router.afterEach((to, from) => {
    document.title = to.meta.title;
});

export default router;
